//
//  ImageTextViewController.m
//  MIDI
//
//  Created by 邵子凡 on 15/10/29.
//  Copyright © 2015年 QXSX. All rights reserved.
//

#import "ImageTextViewController.h"
#import "AppConfig.h"
#import "AddArticleTitileCell.h"
#import "AddTextContentCell.h"
#import "AddImageContentCell.h"
#import "AddImageTextButtonCell.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "UIActionSheet+Blocks.h"
#import "MBProgressHUD+YC.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "UserDataSingleton.h"
#import "CommFunc.h"

@interface ImageTextViewController ()<UITableViewDelegate,UITableViewDataSource,AddImageContentCellDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,AddImageTextButtonCellDelegate,AddArticleTitileCellDelegate,AddTextContentCellDelagate,QXNetWorkToolDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) UIImagePickerController * headPicker;
@property (strong,nonatomic) UIImage *pageTitelImage;
@property (strong,nonatomic) UIImagePickerController * videoPicker;
@property (strong,nonatomic) UIButton *selectedBtn;
@property (strong,nonatomic) NSString *titleText;
@property (strong,nonatomic) NSString *contentText;
@property (strong,nonatomic) UITextView *textViewContent;
@property (strong,nonatomic) NSMutableDictionary * imageDict;
@end

@implementation ImageTextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.backgroundColor = UIColorRGBA(219, 219, 219, 1);
    
    //导航栏返回按钮
    [self settingLeftBarButtonItem];
    
    //导航栏标题
    self.navigationItem.title = @"图文申请";
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 4;
    
}

- (NSMutableDictionary *)imageDict{
    
    if (!_imageDict) {
        
       NSDictionary *imageDict = @{@"image1":@"",@"image2":@"",@"image3":@"",@"textView1":@"",@"textView2":@"",@"textView3":@""};
        
        _imageDict = [NSMutableDictionary dictionaryWithDictionary:imageDict];
    }
    
    return _imageDict;
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        return 44.5;
        
    }
    if (indexPath.row == 1) {
    
        return 275;
        
    }
    if (indexPath.row == 2) {
        
        return 192;
    }
    if (indexPath.row == 3) {
        
        return 87;
    }
    
    return 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        AddArticleTitileCell * cell = [AddArticleTitileCell cellWithTableView:tableView];
        
        cell.delegate = self;
        
        return cell;
        
    }
    if (indexPath.row == 1) {
        
        AddTextContentCell * cell = [AddTextContentCell cellWithTableView:tableView];
        
        cell.delagate = self;
        
        return cell;
    }
    if (indexPath.row == 2) {
        
        AddImageContentCell *cell = [AddImageContentCell cellWithTableView:tableView];
        
        cell.delegate = self;
        
        return cell;
    }
    if (indexPath.row == 3) {
        
        AddImageTextButtonCell *cell = [AddImageTextButtonCell cellWithTableView:tableView];
        
        cell.delegate = self;
        
        return cell;
    }
    
    return nil;
    
}

- (void) uploadTextContent:(AddImageContentCell *)cell Btn:(UIButton *) btn{
    
    self.selectedBtn = btn;
    
    [self upLoadImage];
}

- (void)uploadVideo{
    
    self.videoPicker = [[UIImagePickerController alloc] init];
    
    self.videoPicker.delegate = self;
    
    self.videoPicker.allowsEditing = YES;
    
    self.videoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    self.videoPicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
    
    [self.navigationController presentViewController:self.videoPicker animated:YES completion:nil];
}

- (void)upLoadImage{
    
    self.headPicker = [[UIImagePickerController alloc] init];
    self.headPicker.mediaTypes = @[(NSString *)kUTTypeImage];
    self.headPicker.allowsEditing = YES;
    self.headPicker.delegate = self;
    
    if (/* DISABLES CODE */ (YES)/*[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]*/) {
        WEAKSELF
        [UIActionSheet showInView:self.view withTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"拍照", @"从相册选择"] tapBlock:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {
            switch (buttonIndex) {
                case 0:
                    weakSelf.headPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    weakSelf.headPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                default:
                    break;
            }
            if (buttonIndex != actionSheet.cancelButtonIndex) {
                [weakSelf.navigationController presentViewController:weakSelf.headPicker animated:YES completion:nil];
            }
        }];
    }
    else
    {
        self.headPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self.navigationController presentViewController:self.headPicker animated:YES completion:nil];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if (picker == self.headPicker)
    {
        [picker dismissViewControllerAnimated:YES completion:^{
            
            UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
            
            self.pageTitelImage = editedImage;
            
            if (self.selectedBtn)
            {
                [self.selectedBtn setImage:editedImage forState:UIControlStateNormal];
            }
            
            switch (self.selectedBtn.tag) {
                case 1:
                    
                    [self.imageDict setValue:[CommFunc getImageBase64String:self.pageTitelImage] forKey:@"image1"];
                    
                    break;
                
                case 2:
                    
                    [self.imageDict setValue:[CommFunc getImageBase64String:self.pageTitelImage] forKey:@"image2"];
                  
                    break;
                
                case 3:
                      [self.imageDict setValue:[CommFunc getImageBase64String:self.pageTitelImage] forKey:@"image3"];

                    break;
                
                    
                default:
                    break;
            }
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
            
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }];
    }
    
    if (picker == self.videoPicker)
    {
        [picker dismissViewControllerAnimated:YES completion:^{
            
            NSLog(@"%@",info);
            
            //            [self movToMp4:[NSURL URLWithString:info[UIImagePickerControllerMediaURL]]];
            
            [MBProgressHUD showSuccess:@"成功拉"];
            
        }];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//第一个cell的textField的属性
- (void)upLoadTitle:(NSString *)titleText{
    
    self.titleText = titleText;
}

//第二个cell的textView的属性
- (void)upLoadContent:(NSString *)contentText{
    
    self.contentText = contentText;
}

//第三个cell的textView的属性
- (void)uploadTextViewContent:(UITextView *)textViewContent{
    
    self.textViewContent = textViewContent;
    
    switch (self.textViewContent.tag) {
        case 1:
            
            [self.imageDict setValue:self.textViewContent.text forKey:@"textView1"];
            
            break;
            
        case 2:
            
            [self.imageDict setValue:self.textViewContent.text forKey:@"textView2"];
            
            break;
            
        case 3:
            
            [self.imageDict setValue:self.textViewContent.text forKey:@"textView3"];
            
            break;
        
            
        default:
            break;
    }
}

//申请按钮上传
- (void)uploadImageTextBtn:(AddImageTextButtonCell *)cell{
    
    [self toDownload];
}

- (void)uploadImageText{
    
    if ([self.titleText isEqualToString:@""] || !self.titleText) {
        [MBProgressHUD showError:@"请填写标题"];
        
        return;
    }
    
    if ([self.contentText isEqualToString:@""] || !self.contentText) {
        
        [MBProgressHUD showError:@"请填写正文"];
        
        return;
    }
    
    
    
    [MBProgressHUD showMessage:@"正在加载"];
    
    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
    
    QXnetWorkTool.QXNetWorkDelegate = self;
    
    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:ImageTextViewInterface andParameter:@[@{@"user_id":[UserDataSingleton shareInstance].user_id,@"title":self.titleText,@"content":self.contentText,@"img1":_imageDict[@"image1"],@"remark1":_imageDict[@"textView1"],@"img2":_imageDict[@"image2"],@"remark2":_imageDict[@"textView2"],@"img3":_imageDict[@"image3"],@"remark3":_imageDict[@"textView3"]}] tag:1];
}

/**
 *  上传失败
 */
- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    if (Response == nil)
        return;
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showError:Response];
            break;
    }
}

/**
 *  上传成功
 *
 *  @param message 是服务器返回上传成功的一个值
 *  @param tag     对应是哪个接口参数上传的标记
 */
- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    
    [MBProgressHUD hideHUD];
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showSuccess:message];
            
            break;
    }
}

//提示框 是否确定上传资料？
- (void)toDownload{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"图文申请" message:@"是否确定上传资料" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
    
    [alert addButtonWithTitle:@"确定"];
    
    [alert show];
    
}

//alertView 的代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        
        [self uploadImageText];
        
    }
}

@end
